<?php
require_once '../sub/init.php';
$currfields = 'money,grade,openid,subscribe';
require_once 'tg_chkuser.php';
require_once ZEAI.'cache/config_vip.php';
require_once ZEAI.'cache/config_reg.php';
require_once ZEAI.'cache/config_wxgzh.php';
$TG_set = json_decode(stripslashes($_REG['TG_set']),true);
$data_money    = $row['money'];
$data_grade    = $row['grade'];
$data_openid   = $row['openid'];
$data_subscribe= $row['subscribe'];
//调出组局部参数
$row_role=$db->ROW(__TBL_TG_ROLE__,"reward_kind,tx_min_price,tx_daymax_price,tx_sxf_bfb","grade=".$data_grade,"name");
$reward_kind  = $row_role['reward_kind'];
$tx_min_price    = $row_role['tx_min_price'];
$tx_daymax_price = $row_role['tx_daymax_price'];
$tx_sxf_bfb      = $row_role['tx_sxf_bfb'];

if($reward_kind=='loveb')json_exit(array('flag'=>0,'msg'=>'请返回到'.$_ZEAI['loveB'].'管理'));
$curpage = 'tg_my_money';
$nodatatips = "<div class='nodatatips'><i class='ico'>&#xe608;</i>暂时没有内容</div>";
/*************Ajax Start*************/
if($submitok == 'ajax_tg_gzh_ewm'){
	$token = wx_get_access_token();
	if(str_len($token) < 50)json_exit(array('flag'=>0,'tgpic'=>'','msg'=>'zeai_error_token'));
	$ticket_url  = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$token;
	$ticket_data = '{"action_name":"QR_LIMIT_STR_SCENE","action_info":{"scene":{"scene_str":"www_ZEAI_cn_tghn_'.$cook_tg_uid.'"}}}';
	$ticket      = Zeai_POST_stream($ticket_url,$ticket_data);
	$T           = json_decode($ticket,true);
	$qrcode_url  = 'http://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($T['ticket']);
	$dbdir  = 'p/tmp/'.date('Y').'/'.date('m').'/';
	@mk_dir(ZEAI.'/up/'.$dbdir);
	$dbname = $dbdir.$cook_tg_uid.'_'.cdstrletters(3).'.jpg';
	$DST    = ZEAI.'/up/'.$dbname;
	$im=imagecreatefromjpeg($qrcode_url);
	imagejpeg($im,$DST,90);
	imagedestroy($im);
	json_exit(array('flag'=>1,'qrcode_url'=>UP.$dbname));
}elseif($submitok == 'ajax_tx_update' && !empty($TG_set['tx_num_list'])){
	if(!ifint($tx_money,"0-9","1,9"))json_exit(array('flag'=>0,'msg'=>'提现的金额必须是正整数'));
	if($tx_money<$tx_min_price)json_exit(array('flag'=>0,'msg'=>'提现金额必须大于'.$tx_min_price.'元'));
	$tx_money = abs(floatval($tx_money));
	if($tx_money > $data_money)json_exit(array('flag'=>0,'msg'=>'账户余额不足'.$tx_money));
	if($tx_daymax_price>0 && $tx_money>$tx_daymax_price)json_exit(array('flag'=>0,'msg'=>'每天最多提现'.$tx_daymax_price));
	//
	$today = YmdHis(ADDTIME,'Ymd');
	$rt=$db->query("SELECT SUM(money) AS txallmoney FROM ".__TBL_PAY__." WHERE kind=-1 AND date_format(from_unixtime(addtime),'%Y-%m-%d') = '$today' AND tg_uid=".$cook_tg_uid);
	$row=$db->fetch_array($rt,'name');
	$txallmoney=abs($row['txallmoney']);
	if($tx_daymax_price>0 && ($txallmoney >= $tx_daymax_price) || $tx_money > ($tx_daymax_price - $txallmoney)  )json_exit(array('flag'=>0,'msg'=>'亲！每天最多提现 ￥'.$tx_daymax_price.' 哦'));
	$endnum  = $data_money-$tx_money;
	$orderid = "TGTX_".$cook_tg_uid."_".$tx_money."_".date("Ymdhis");
	$db->query("UPDATE ".__TBL_TG_USER__." SET money=".$endnum." WHERE money>=".$tx_money." AND id=".$cook_tg_uid);
	//写清单
	$title   = "推广申请提现(手机)";
	$content = "推广申请提现<span class=\"Cf60 S12\">（等待处理）</span>";
	$money_list_id = $db->AddLovebRmbList($cook_tg_uid,$content,-$tx_money,'money',2,'tg');
	$paymoney = floatval((1-$tx_sxf_bfb/100)*$tx_money);
	//生成提现记录
	$db->query("INSERT INTO ".__TBL_PAY__." (tg_uid,orderid,money_list_id,kind,title,money,paymoney,addtime) VALUES ($cook_tg_uid,'$orderid',$money_list_id,-1,'$title','$tx_money','$paymoney',".ADDTIME.")");
	//站内消息
	$C = '您的推广余额账户资金有变动！';
	$db->SendTip($cook_tg_uid,'推广余额提现',dataIO($C,'in'),'tg');
	//账户资金变动提醒
	if (!empty($data_openid) && $data_subscribe==1){
		$first  = urlencode("您的推广余额账户资金有变动：");
		$remark = urlencode("申请推广提现扣除");
		@wx_mb_sent('mbbh=ZEAI_ACCOUNT_CHANGE&openid='.$data_openid.'&num=-'.$tx_money.'&endnum='.$endnum.'&first='.$first.'&remark='.$remark.'&url=');
	}
	json_exit(array('flag'=>1,'msg'=>'提现申请已提交，请等待处理','tx_money'=>$tx_money));
}
/*************Ajax End*************/
$nav = 'tg_my';
$a = (empty($a))?'ye':$a;?>
<!doctype html><html><head><meta charset="utf-8">
<title><?php echo $headertitle;?><?php echo $_ZEAI['siteName'];?></title>
<?php echo HEADMETA; ?>
<meta name="generator" content="Zeai.cn V9" />
<link href="<?php echo RES;?>m/css/m.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<link href="<?php echo RES;?>m/css/TG2.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<script src="<?php echo RES;?>www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo RES;?>m/js/m.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<style>
body{background-color:#fff}
.tabmenufix{top:6.7rem}
.tabmenu a.ed:after{background-color:#EE5A4E}
</style>
</head>
<body>
<?php 
$mini_class='top_mini4 bgcolor yunbox';
$mini_title='我的账户';
require_once ZEAI.'m/top_mini.php';?>
<div class="tabmenu tabmenu<?php echo (!empty($TG_set['tx_num_list']))?3:2;?> tabmenufix">
    <a href="<?php echo SELF;?>"<?php echo ($a == 'ye')?' class="ed"':''; ?>>账户余额</a>
    <a href="<?php echo SELF;?>?a=mx"<?php echo ($a == 'mx')?' class="ed"':''; ?>>账户明细</a>
    <?php if(!empty($TG_set['tx_num_list'])){ ?>
        <a href="<?php echo SELF;?>?a=tx"<?php echo ($a == 'tx')?' class="ed"':''; ?>>提现</a>
    <?php }?>
</div>
<?php
if($a == 'tx'){
	if(empty($TG_set['tx_num_list']))exit('<div class="nodatatips">提现已关闭</div>');?>
	<div class="tg_my_money_tx">
		<h3><b>请选择提现金额</b><br><span class="S12 C999">当前账户余额：<font class="Cf00"><?php echo str_replace(".00","",$data_money);; ?>元</font></span></h3>
        <form id="z_e_a_i__c_n__tx_form"><?php
			$ARR = explode(',',$TG_set['tx_num_list']);
			if (@count($ARR) >= 1 && is_array($ARR)){
				$n=1;
				foreach ($ARR as $k=>$bV) {
					$cls=($k==0)?' checked':'';
					$V=intval($bV);?>
                    <li><input type="radio" name="tx_money" id="tx_money<?php echo $n;?>" class="radioskin" value="<?php echo $V;?>"<?php echo $cls;?>><label for="tx_money<?php echo $n;?>" class="radioskin-label"><i class="i1"></i><b class="W80 S16"><?php echo $V;?>元</b></label></li>
                    <?php
					$n++;
				}
			}?>
        	<input type="hidden" name="submitok" value="ajax_tx_update">
        </form>
		<div class="clear"></div>
       <?php if ($tx_sxf_bfb>0){ ?><h5>提现将收取<?php echo $tx_sxf_bfb; ?>%的手续费</h5><?php }?>
       <div style="text-align:center;margin:2rem auto"><button type="button" class="btn size4 <?php if (!empty($_GZH['wx_gzh_ewm']) && ($data_subscribe!=1)){echo 'HUI';}else{echo 'HONG4';};?> W85_ yuan" id="txbtn"<?php if (!empty($_GZH['wx_gzh_ewm']) && ($data_subscribe!=1))echo ' disabled';?>>立即提现</button></div>
        <div class="linebox">
            <div class="line W50"></div>
            <div class="title BAI S14">温馨提醒</div>
        </div>
        <h6>为了补贴网站运营和服务器成本，我们将扣除部分手续费<?php echo $tx_sxf_bfb; ?>％，实际到账<?php echo (100-$tx_sxf_bfb); ?>％，有小数的将取整处理</h6>
        
        <div id="subscribe_box_my_money_tx" class="my-subscribe_box"><font class="S16 B">微信扫码关注公众号</font><img id="tghn_gzh_ewm" src="<?php echo HOST.'/res/loadingData.gif'; ?>">
            <h3>
            <?php if (!is_weixin()){?>请用微信打开使用提现功能<br><?php }?>
            <?php if (is_weixin()){?>关注成功才能打款给您的微信零钱<?php }?>
            </h3>
        </div>
		<script>
        txbtn.onclick = function(){
            zeai.confirm('确定要提现么？<br><br>请确定是否已经关注本站公众号，否则无法打款至您的微信钱包<br>',function (){zeai.ajax({'url':'tg_my_money'+zeai.extname,'js':1,'form':z_e_a_i__c_n__tx_form},function(e){var rs=zeai.jsoneval(e);
				if (rs.flag==1){
					zeai.msg(rs.msg);
					setTimeout(function(){location.reload(true);},1000);
				}else{
					zeai.msg(rs.msg);
				}
			});});
        }
		<?php
		if ($data_subscribe != 1){
			@wx_endurl_tg('您刚刚浏览的页面【'.$TG_set['navtitle'].'-余额提现】',HOST.'/m/tg_my_money.php');
			?>
			setTimeout(function(){
				zeai.ajax({url:HOST+'/m/tg_my_money'+zeai.ajxext+'submitok=ajax_tg_gzh_ewm'},function(e){var rs=zeai.jsoneval(e);
					if(rs.flag==1){
						tghn_gzh_ewm.src=rs.qrcode_url;
						tghn_gzh_ewm.onload=function(){ZeaiM.div({obj:o('subscribe_box_my_money_tx'),w:26,h:28});}
					}
				});
			},500);
		<?php }?>
        </script>
    </div>
	<?php
}elseif($a == 'mx'){?>
	<style>
		.tg_my_money_mx{padding:1rem 0;margin-top:11rem}
		.tg_my_money_mx dl{width:96%;padding:.5rem 1.5rem;box-sizing:border-box;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;clear:both;overflow:auto;border-bottom:#f8f8f8 1px solid;margin:0 auto}
		.tg_my_money_mx dl:last-child{border-bottom:0}
		.tg_my_money_mx dl dt,.tg_my_money_mx dl dd{float:left;display:block}
		.tg_my_money_mx dl:hover{background-color:#f8f8f8}
		.tg_my_money_mx dl dt{width:70%;font-size:1.4rem;text-align:left;line-height:150%}
		.tg_my_money_mx dl dt font{color:#999;font-size:1.2rem;display:block}
		.tg_my_money_mx dl dd{width:28%;text-align:right;font-size:1.6rem;line-height:3.9rem}
		.tg_my_money_mx dl dd.zs{color:#EE5A4E}
		.tg_my_money_mx dl dd.fs{color:#097AFE}
    </style>
    <div class="tg_my_money_mx" id="mx">
		<?php
        $p = intval($p);if ($p<1)$p=1;$SQL="";$totalP = intval($totalP);
        if($i=='tx'){
            $TGsql = " AND kind=2 ";	
        }elseif($i=='sy'){
            $TGsql = " AND kind<>2 ";	
        }
        $rt = $db->query("SELECT id,content,num,addtime FROM ".__TBL_MONEY_LIST__." WHERE tg_uid=".$cook_tg_uid.$TGsql." ORDER BY id DESC ".$SQL);
        $total = $db->num_rows($rt);
        if ($total <= 0){
            echo($nodatatips);
        }else{
            if ($p == 1){
                $fort= $total;
            }else{
                $fort= $_ZEAI['pagesize'];
                $db->data_seek($rt,($p-1)*$_ZEAI['pagesize']);
            }
            for($n=1;$n<=$fort;$n++) {
                $rows = $db->fetch_array($rt,'name');
                if(!$rows)break;
                $id       = $rows['id'];
                $content  = dataIO($rows['content'],'out');
                $num      = str_replace(".00","",$rows['num']);;
                $addtime  = YmdHis($rows['addtime']);
                if ($num<0){$numstyle = "fs";}else{$numstyle = "zs";$num = '+'.$num;}
                echo '<dl><dt>'.$content.'<font>'.$addtime.'</font></dt><dd class="'.$numstyle.'">'.$num.'</dd></dl>';
            }
        }?>
    </div>
    <?php
}else{?>
	<style>
    .tg_my_money_ye{padding:1px;margin-top:9rem}
    .tg_my_money_ye .box{width:80%;margin:5rem auto 3rem auto}
    .tg_my_money_ye .box .dt{width:30%;float:left;border-right:#eee 1px solid;text-align:left}
    .tg_my_money_ye .box .dt i{color:#EE5A4E;font-size:7rem}
    .tg_my_money_ye .box .dd{width:64%;float:right;text-align:left}
    .tg_my_money_ye .money{color:#EE5A4E;font:normal 3.6rem/3.6rem Arial;display:block}
    .tg_my_money_ye .money font{color:#8d8d8d;font-size:2rem;padding-left:.3rem}
    .tg_my_money_ye .money:before{content:'当前余额';font-size:1.5rem;display:block;color:#999;font-family:'Microsoft YaHei','SimSun','宋体'}
    .tg_my_money_ye .moneydetail{margin:2rem auto;color:#8d8d8d;line-height:200%;text-align:left;display:inline-block;font-size:1.2rem}
    .tg_my_money_ye .moneydetail .size1{padding:0 1rem}
    </style>
    <div class="tg_my_money_ye">
        <div class="box">
            <div class="dt"><i class="ico">&#xe61a;</i></div>
            <div class="dd"><span class="money"><?php echo str_replace(".00","",$data_money);; ?><font>元</font></span></div>
			<?php if(!empty($TG_set['tx_num_list'])){ ?>
            <div class="moneydetail"><sapn>可提现至您的微信钱包</span>　<a href="tg_my_money.php?a=tx" class="btn size1 yuan BAI">立即提现</a></div>
            <?php }?>
            
        </div>
    </div>    
	<?php
}
require_once ZEAI.'m/tg_bottom.php';
?>